/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    tim.c
  * @brief   This file provides code for the configuration
  *          of the TIM instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "tim.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim15;
TIM_HandleTypeDef htim20;


/* TIM1 init function - 修改为主触发源 */
void MX_TIM1_Init(void)
{
    TIM_ClockConfigTypeDef sClockSourceConfig = {0};
    TIM_MasterConfigTypeDef sMasterConfig = {0};
    TIM_OC_InitTypeDef sConfigOC = {0};
    TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};

    /* USER CODE BEGIN TIM1_Init 0 */
    // TIM1配置为主触发源，触发TIM3
    /* USER CODE END TIM1_Init 0 */

    htim1.Instance = TIM1;
    htim1.Init.Prescaler = 1699;         // 预分频1700，计数频率100kHz
    htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim1.Init.Period = 19;              // 自动重装载20，PWM频率5kHz
    htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    htim1.Init.RepetitionCounter = 0;
    htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
    
    if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
    {
        Error_Handler();
    }
    
    sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
    if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
    {
        Error_Handler();
    }
    
    if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
    {
        Error_Handler();
    }
    
    // 配置主模式，输出触发信号
    sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
    sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
    if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
    {
        Error_Handler();
    }
    
    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = 8;                 // 20*0.4=8，占空比40%
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
    sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
    sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
    if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
    {
        Error_Handler();
    }
    
    sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
    sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
    sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
    sBreakDeadTimeConfig.DeadTime = 0;
    sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
    sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
    sBreakDeadTimeConfig.BreakFilter = 0;
    sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT;
    sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
    sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
    sBreakDeadTimeConfig.Break2Filter = 0;
    sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
    sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
    if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
    {
        Error_Handler();
    }
    
    /* USER CODE BEGIN TIM1_Init 2 */
    // TIM1配置完成，可以作为主触发源
    /* USER CODE END TIM1_Init 2 */
    
    HAL_TIM_MspPostInit(&htim1);
}

/* TIM3 init function - 修改为内部触发模式 */
void MX_TIM3_Init(void)
{
    TIM_ClockConfigTypeDef sClockSourceConfig = {0};
    TIM_SlaveConfigTypeDef sSlaveConfig = {0};
    TIM_MasterConfigTypeDef sMasterConfig = {0};
    TIM_OC_InitTypeDef sConfigOC = {0};

    /* USER CODE BEGIN TIM3_Init 0 */
    // TIM3配置为输出比较模式，生成脉冲信号
    /* USER CODE END TIM3_Init 0 */

    htim3.Instance = TIM3;
    htim3.Init.Prescaler = 16999;        // 预分频17000，计数频率10kHz
    htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim3.Init.Period = 9999;            // 自动重装载10000，频率1Hz
    htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
    
    if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
    {
        Error_Handler();
    }
    
    sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
    if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
    {
        Error_Handler();
    }
    
    if (HAL_TIM_OC_Init(&htim3) != HAL_OK)
    {
        Error_Handler();
    }
    
    // 配置为从模式，由TIM1触发
    sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
    sSlaveConfig.InputTrigger = TIM_TS_ITR0;  // TIM1作为触发源
    if (HAL_TIM_SlaveConfigSynchro(&htim3, &sSlaveConfig) != HAL_OK)
    {
        Error_Handler();
    }
    
    // 配置主模式，输出触发信号给TIM15
    sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
    if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
    {
        Error_Handler();
    }
    
    sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
    sConfigOC.Pulse = 0;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
    if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
    {
        Error_Handler();
    }
    
    /* USER CODE BEGIN TIM3_Init 2 */
    // TIM3配置完成，可以生成脉冲信号
    /* USER CODE END TIM3_Init 2 */
}

/* TIM15 init function - 修改为内部触发模式 */
void MX_TIM15_Init(void)
{
    TIM_ClockConfigTypeDef sClockSourceConfig = {0};
    TIM_SlaveConfigTypeDef sSlaveConfig = {0};
    TIM_IC_InitTypeDef sConfigIC = {0};

    /* USER CODE BEGIN TIM15_Init 0 */
    // TIM15配置为输入捕获模式，测量TIM3的脉冲信号
    /* USER CODE END TIM15_Init 0 */

    htim15.Instance = TIM15;
    htim15.Init.Prescaler = 169;          // 预分频170，计数频率1MHz
    htim15.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim15.Init.Period = 0xFFFF;         // 最大计数值
    htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    htim15.Init.RepetitionCounter = 0;
    htim15.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
    
    if (HAL_TIM_IC_Init(&htim15) != HAL_OK)
    {
        Error_Handler();
    }
    
    sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
    if (HAL_TIM_ConfigClockSource(&htim15, &sClockSourceConfig) != HAL_OK)
    {
        Error_Handler();
    }
    
    // 配置为从模式，由TIM3触发
    sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
    sSlaveConfig.InputTrigger = TIM_TS_ITR2;  // TIM3作为触发源
    if (HAL_TIM_SlaveConfigSynchro(&htim15, &sSlaveConfig) != HAL_OK)
    {
        Error_Handler();
    }
    
    // 配置输入捕获通道1
    sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
    sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
    sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
    sConfigIC.ICFilter = 0;
    if (HAL_TIM_IC_ConfigChannel(&htim15, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
    {
        Error_Handler();
    }
    
    // 启动输入捕获中断
    HAL_TIM_IC_Start_IT(&htim15, TIM_CHANNEL_1);
    
    /* USER CODE BEGIN TIM15_Init 2 */
    // TIM15配置完成，可以测量脉冲信号
    /* USER CODE END TIM15_Init 2 */
}


/* TIM20 init function */
void MX_TIM20_Init(void)
{

  /* USER CODE BEGIN TIM20_Init 0 */

  /* USER CODE END TIM20_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM20_Init 1 */

  /* USER CODE END TIM20_Init 1 */
  htim20.Instance = TIM20;
  htim20.Init.Prescaler = 1699;
  htim20.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim20.Init.Period = 199;   // 100000 / 200 = 500 HZ 也就是 0.002s 也就是 2ms
  htim20.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim20.Init.RepetitionCounter = 0;
  htim20.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim20) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim20, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim20, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM20_Init 2 */

  /* USER CODE END TIM20_Init 2 */

}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(tim_baseHandle->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspInit 0 */

  /* USER CODE END TIM1_MspInit 0 */
    /* TIM1 clock enable */
    __HAL_RCC_TIM1_CLK_ENABLE();
  /* USER CODE BEGIN TIM1_MspInit 1 */

  /* USER CODE END TIM1_MspInit 1 */
  }
  else if(tim_baseHandle->Instance==TIM3)
  {
  /* USER CODE BEGIN TIM3_MspInit 0 */

  /* USER CODE END TIM3_MspInit 0 */
    /* TIM3 clock enable */
    __HAL_RCC_TIM3_CLK_ENABLE();

    /* TIM3 interrupt Init */
    HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM3_IRQn);
  /* USER CODE BEGIN TIM3_MspInit 1 */

  /* USER CODE END TIM3_MspInit 1 */
  }
  else if(tim_baseHandle->Instance==TIM15)
  {
  /* USER CODE BEGIN TIM15_MspInit 0 */

  /* USER CODE END TIM15_MspInit 0 */
    /* TIM15 clock enable */
    __HAL_RCC_TIM15_CLK_ENABLE();

    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**TIM15 GPIO Configuration
    PB14     ------> TIM15_CH1
    */
    GPIO_InitStruct.Pin = GPIO_PIN_14;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM15;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* TIM15 interrupt Init */
    HAL_NVIC_SetPriority(TIM1_BRK_TIM15_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM1_BRK_TIM15_IRQn);
  /* USER CODE BEGIN TIM15_MspInit 1 */

  /* USER CODE END TIM15_MspInit 1 */
  }
  else if(tim_baseHandle->Instance==TIM20)
  {
  /* USER CODE BEGIN TIM20_MspInit 0 */

  /* USER CODE END TIM20_MspInit 0 */
    /* TIM20 clock enable */
    __HAL_RCC_TIM20_CLK_ENABLE();

    /* TIM20 interrupt Init */
    HAL_NVIC_SetPriority(TIM20_UP_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM20_UP_IRQn);
  /* USER CODE BEGIN TIM20_MspInit 1 */

  /* USER CODE END TIM20_MspInit 1 */
  }
}

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(timHandle->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspPostInit 0 */

  /* USER CODE END TIM1_MspPostInit 0 */

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**TIM1 GPIO Configuration
    PA8     ------> TIM1_CH1
    */
    GPIO_InitStruct.Pin = GPIO_PIN_8;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF6_TIM1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN TIM1_MspPostInit 1 */

  /* USER CODE END TIM1_MspPostInit 1 */
  }

}




void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{

  if(tim_baseHandle->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspDeInit 0 */

  /* USER CODE END TIM1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM1_CLK_DISABLE();

    /* TIM1 interrupt Deinit */
  /* USER CODE BEGIN TIM1:TIM1_BRK_TIM15_IRQn disable */
    /**
    * Uncomment the line below to disable the "TIM1_BRK_TIM15_IRQn" interrupt
    * Be aware, disabling shared interrupt may affect other IPs
    */
    /* HAL_NVIC_DisableIRQ(TIM1_BRK_TIM15_IRQn); */
  /* USER CODE END TIM1:TIM1_BRK_TIM15_IRQn disable */

  /* USER CODE BEGIN TIM1_MspDeInit 1 */

  /* USER CODE END TIM1_MspDeInit 1 */
  }
  else if(tim_baseHandle->Instance==TIM3)
  {
  /* USER CODE BEGIN TIM3_MspDeInit 0 */

  /* USER CODE END TIM3_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM3_CLK_DISABLE();

    /* TIM3 interrupt Deinit */
    HAL_NVIC_DisableIRQ(TIM3_IRQn);
  /* USER CODE BEGIN TIM3_MspDeInit 1 */

  /* USER CODE END TIM3_MspDeInit 1 */
  }
  else if(tim_baseHandle->Instance==TIM15)
  {
  /* USER CODE BEGIN TIM15_MspDeInit 0 */

  /* USER CODE END TIM15_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM15_CLK_DISABLE();

    /**TIM15 GPIO Configuration
    PB14     ------> TIM15_CH1
    */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_14);

    /* TIM15 interrupt Deinit */
  /* USER CODE BEGIN TIM15:TIM1_BRK_TIM15_IRQn disable */
    /**
    * Uncomment the line below to disable the "TIM1_BRK_TIM15_IRQn" interrupt
    * Be aware, disabling shared interrupt may affect other IPs
    */
    /* HAL_NVIC_DisableIRQ(TIM1_BRK_TIM15_IRQn); */
  /* USER CODE END TIM15:TIM1_BRK_TIM15_IRQn disable */

  /* USER CODE BEGIN TIM15_MspDeInit 1 */

  /* USER CODE END TIM15_MspDeInit 1 */
  }
  else if(tim_baseHandle->Instance==TIM20)
  {
  /* USER CODE BEGIN TIM20_MspDeInit 0 */

  /* USER CODE END TIM20_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM20_CLK_DISABLE();

    /* TIM20 interrupt Deinit */
    HAL_NVIC_DisableIRQ(TIM20_UP_IRQn);
  /* USER CODE BEGIN TIM20_MspDeInit 1 */

  /* USER CODE END TIM20_MspDeInit 1 */
  }
}




/* USER CODE BEGIN 1 */

// TIM3和TIM15测量相关变量
static uint32_t tim3_pulse_count = 0;       // TIM3输出的脉冲计数
static uint32_t tim15_pulse_count = 0;      // TIM15测量到的脉冲计数
static uint32_t tim15_last_capture = 0;     // TIM15上次捕获值
static uint32_t tim15_frequency = 0;        // TIM15测量到的频率
static uint32_t tim15_period = 0;           // TIM15测量到的周期
static uint8_t tim15_first_capture = 1;     // TIM15首次捕获标志

/**
 * @brief TIM3脉冲信号生成配置
 * @param pulse_per_second: 每秒脉冲数 (1, 10, 100)
 * @retval None
 */
void TIM3_SetPulseOutput(uint32_t pulse_per_second)
{
    uint32_t period;
    
    // 计算周期值
    switch(pulse_per_second)
    {
        case 1:
            period = 9999;   // 10kHz / 10000 = 1Hz
            break;
        case 10:
            period = 999;    // 10kHz / 1000 = 10Hz
            break;
        case 100:
            period = 99;     // 10kHz / 100 = 100Hz
            break;
        default:
            period = 9999;   // 默认1Hz
            break;
    }
    
    // 停止定时器
    HAL_TIM_OC_Stop(&htim3, TIM_CHANNEL_1);
    
    // 重新配置定时器
    htim3.Init.Period = period;
    HAL_TIM_Base_Init(&htim3);
    
    // 重新启动输出比较
    HAL_TIM_OC_Start_IT(&htim3, TIM_CHANNEL_1);
}

/**
 * @brief 启动TIM3脉冲信号输出
 * @param None
 * @retval None
 */
void TIM3_StartPulseOutput(void)
{
    tim3_pulse_count = 0;
    HAL_TIM_OC_Start_IT(&htim3, TIM_CHANNEL_1);
}

/**
 * @brief 停止TIM3脉冲信号输出
 * @param None
 * @retval None
 */
void TIM3_StopPulseOutput(void)
{
    HAL_TIM_OC_Stop_IT(&htim3, TIM_CHANNEL_1);
}

/**
 * @brief 获取TIM3输出的脉冲计数
 * @param None
 * @retval 脉冲计数
 */
uint32_t TIM3_GetPulseCount(void)
{
    return tim3_pulse_count;
}

/**
 * @brief 重置TIM3脉冲计数
 * @param None
 * @retval None
 */
void TIM3_ResetPulseCount(void)
{
    tim3_pulse_count = 0;
}

/**
 * @brief 启动TIM15输入捕获测量
 * @param None
 * @retval None
 */
void TIM15_StartMeasurement(void)
{
    tim15_pulse_count = 0;
    tim15_first_capture = 1;
    tim15_frequency = 0;
    tim15_period = 0;
    HAL_TIM_IC_Start_IT(&htim15, TIM_CHANNEL_1);
}

/**
 * @brief 停止TIM15输入捕获测量
 * @param None
 * @retval None
 */
void TIM15_StopMeasurement(void)
{
    HAL_TIM_IC_Stop_IT(&htim15, TIM_CHANNEL_1);
}

/**
 * @brief 获取TIM15测量到的脉冲计数
 * @param None
 * @retval 脉冲计数
 */
uint32_t TIM15_GetPulseCount(void)
{
    return tim15_pulse_count;
}

/**
 * @brief 获取TIM15测量到的频率
 * @param None
 * @retval 频率(Hz)
 */
uint32_t TIM15_GetFrequency(void)
{
    return tim15_frequency;
}

/**
 * @brief 获取TIM15测量到的周期
 * @param None
 * @retval 周期(us)
 */
uint32_t TIM15_GetPeriod(void)
{
    return tim15_period;
}

/**
 * @brief 重置TIM15测量数据
 * @param None
 * @retval None
 */
void TIM15_ResetMeasurement(void)
{
    tim15_pulse_count = 0;
    tim15_first_capture = 1;
    tim15_frequency = 0;
    tim15_period = 0;
}

/**
 * @brief TIM3输出比较中断回调函数
 * @param htim: 定时器句柄
 * @retval None
 */
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
    if(htim->Instance == TIM3)
    {
        tim3_pulse_count++;
    }
}

/**
 * @brief TIM15输入捕获中断回调函数
 * @param htim: 定时器句柄
 * @retval None
 */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
    if(htim->Instance == TIM15)
    {
        uint32_t current_capture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
        
        if(tim15_first_capture)
        {
            tim15_last_capture = current_capture;
            tim15_first_capture = 0;
        }
        else
        {
            uint32_t diff;
            
            // 处理计数器溢出情况
            if(current_capture >= tim15_last_capture)
            {
                diff = current_capture - tim15_last_capture;
            }
            else
            {
                diff = (0xFFFF - tim15_last_capture) + current_capture + 1;
            }
            
            // 计算周期（微秒）TIM15预分频170，计数频率1MHz，每个计数=1us
            tim15_period = diff;
            
            // 计算频率（Hz）
            if(tim15_period > 0)
            {
                tim15_frequency = 1000000 / tim15_period;
            }
            
            tim15_last_capture = current_capture;
        }
        
        tim15_pulse_count++;
    }
}

/* USER CODE END 1 */
